﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using SYH.Utils;

namespace syh876.com.Areas.User.Controllers
{
    public class MessageController : Controller
    {
        //
        // GET: /User/Message/

        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 发件箱
        /// </summary>
        /// <returns></returns>
        public ActionResult MessageSender()
        {
            int pageIndex, pageCount, recordCount;
            PagerSource ps = new PagerSource("p", 10);
            pageIndex = ps.CustomPage;
            string sqlWhere = string.Empty;
            DataTable dt = SYH.Public.BLL.Public_MessagesSenders.GetList(10, pageIndex, 1, out pageCount, out recordCount, sqlWhere);
           
            ViewBag._Count = recordCount;
            return View(dt);
        }
        /// <summary>
        /// 收件箱
        /// </summary>
        /// <returns></returns>
        public ActionResult MessageReceiver(string MsgStatus, string MsgTime, string type, int RecordCount)
        {
            int pageCount;
            PagerSource ps = new PagerSource("p", 10);
            int PageIndex = ps.CustomPage;
            string sqlwhere = "";
            if (MsgStatus != "")
            {
                sqlwhere += " and IsReader=" + MsgStatus;
               
            }
            if (MsgTime != "")
            {
                sqlwhere += " and CreateTime < DateAdd(Month,-3,getdate())";
               
            }
            if (type != "")
            {
                sqlwhere += " and TypeId in(" + type + ")";
                
            }
            DataSet ds = SYH.Public.BLL.Public_MessagesReceivers.GetReceiverList(10, PageIndex, out pageCount, out RecordCount, 1, 1, sqlwhere);
            ViewBag.RecordCount = RecordCount;
            DataSet DtSenders =new DataSet();
            int NotReadCount = ds.Tables[0].Select("IsReader=0").Length;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string mailIds = string.Empty;
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    mailIds += row["MailId"] + ",";
                }
                 DtSenders =SYH.Public.BLL.Public_MessagesSenders.SearchSenders(mailIds.Substring(0, mailIds.Length - 1));
                 //ViewBag.Receiver = SYH.Public.BLL.Public_MessagesReceivers.GetReceiverInfo(DtSenders, ds.Tables[0].Rows[]["MailId"]);
            }

            
            return View(ds.Tables[0]);
        }

        /// <summary>
        /// 站内信详情
        /// </summary>
        /// <returns></returns>
        public ActionResult MessageDetail()
        {
            return View();
        }

        /// <summary>
        /// 系统消息
        /// </summary>
        /// <returns></returns>
        public ActionResult MessageSystem(string msgstatus,string msgtime,string type,int RecordCount)
        {
            int pageCount;
            int PageIndex=0;

            string sqlwhere = "";
            if (msgstatus != "")
            {
                sqlwhere += " and IsReader=" + msgstatus;
              
            }
            if (msgtime != "")
            {
                sqlwhere += " and CreateTime < DateAdd(Month,-3,getdate())";
               
            }
            if (type != "")
            {
                sqlwhere += " and TypeId in(" + type + ")";
              
            }
            SYH.Public.BLL.Public_MessagesReceivers bllMsg = new SYH.Public.BLL.Public_MessagesReceivers();
            DataTable ds = SYH.Public.BLL.Public_MessagesReceivers.GetReceiverList(10, PageIndex, out pageCount, out RecordCount, 1, 0, sqlwhere).Tables[0];

            int NotReadCount = ds.Select("IsReader=0").Length;
            string[] Str = new string[NotReadCount];
            for (int i = 0; i < NotReadCount; i++)
            {
                Str[i] = ds.Select("IsReader=0")[i][0].ToString();
            }
            SYH.Public.BLL.Public_MessageCount.UpdateMsgCount(1, NotReadCount);
            bllMsg.UpdateIsReadered(Str);
            return View(ds);
        }

        [HttpPost]
        public ActionResult Count()
        {
            int count = SYH.Public.BLL.Public_MessageCount.GetNoReadReceivedMessage(1/*syh876.com.Areas.User.Code.IdentityHelper.ClientUser.UserId*/);
            int sysCount = SYH.Public.BLL.Public_MessageCount.GetNoReadCountByType(1, "1,2,3,4,5,6,7,8,9,10,14,15,16");
            int yjCount = SYH.Public.BLL.Public_MessageCount.GetNoReadCountByType(1, "11");
            int bjCount = SYH.Public.BLL.Public_MessageCount.GetNoReadCountByType(1, "12");
            int webCount = SYH.Public.BLL.Public_MessageCount.GetNoReadCountByType(1, "13");
            int userCount = SYH.Public.BLL.Public_MessageCount.GetNoReadCountByType(1, "0");
            int tsCount = SYH.Public.BLL.Public_MessageCount.GetNoReadCountByType(1, "17");
            return Content("({\"Count\":\"" + count + "\",\"SysCount\":\"" + sysCount + "\",\"YjCount\":\"" + yjCount + "\",\"BjCount\":\"" + bjCount + "\",\"WebCount\":\"" + webCount + "\",\"UserCount\":\"" + userCount + "\",\"TsCount\":\"" + tsCount + "\"})");    //返回值给wp
        }

        [HttpPost]
        public ActionResult DeleteList(string type, string id)
        {
            int result = SYH.Public.BLL.Public_MessagesReceivers.DeleteReceiverList(id);
            if (result != 0)
            {
                return Content("删除成功！");
                   
            }
            else
            {
                return Content("删除失败！");
            }
        }

        [HttpPost]
        public ActionResult DeleteSender(string id)
        {
            return View();
        }
    }
}
